﻿using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.LHRAF.Models;
using System.Web.Security;

namespace BenzInfo.Hockey.LHRAF.Controllers
{
    public class UsersController : BaseController
    {
        //
        // GET: /User/
        private LHRAFContext db = new LHRAFContext();

        public ViewResult Index()
        {
            return View(db.Users.OrderBy(u => u.Position).ToList());
        }

        public ViewResult Details(int id)
        {
            User user = db.Users.Find(id);
            return View(user);
        }
        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return PartialView();
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(User user, HttpPostedFileBase inputFile)
        {
            if (ModelState.IsValid)
            {
                user.Position = db.Users.Count() + 1;

                db.Users.Add(user);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(user);
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id)
        {
            User user = db.Users.Find(id);
            return PartialView(user);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(User user, HttpPostedFileBase inputFile, string nopicture)
        {

            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(user);
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id)
        {
            User user = db.Users.Find(id);
            return PartialView(user);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            User user = db.Users.Find(id);
            db.Users.Remove(user);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        // GET: /Account/LogOn

        public ActionResult LogOn()
        {
            return PartialView();
        }

        //
        // POST: /Account/LogOn

        [HttpPost]
        public ActionResult LogOn(User user)
        {
            try
            {
                User fullUser = db.Users.Where(m => m.UserName == user.UserName && m.Password == user.Password).First();

                Session["User"] = fullUser;
            }
            catch
            {
                ModelState.AddModelError("", "The user login or password provided is incorrect.");
            }

            return Redirect(Request.UrlReferrer.ToString());
        }

        public ActionResult LogOff()
        {
            Session["User"] = null;
            return Redirect(Request.UrlReferrer.ToString());
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
